using System;
using System.Security.Cryptography;
using System.Text;

namespace WIT.Global;

/// <summary>
/// 加密/解密帮助类
/// </summary>
public class EncryptHelper
{
	/// <summary>
	/// 16位MD5加密
	/// </summary>
	/// <param name="content">需要加密的内容</param>
	/// <param name="upper">是否大写</param>
	/// <returns></returns>
	public static string MD5Encrypt16(string content, bool upper = false)
	{
		string text = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(content)), 4, 8);
		text = text.Replace("-", "");
		if (!upper)
		{
			text = text.ToLower();
		}
		return text;
	}

	/// <summary>
	/// 32位MD5加密
	/// </summary>
	/// <param name="content">需要加密的内容</param>
	/// <param name="upper">是否大写</param>
	/// <returns></returns>
	public static string MD5Encrypt32(string content, bool upper = false)
	{
		string text = string.Empty;
		byte[] array = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(content));
		string text2 = (upper ? "X" : "x");
		for (int i = 0; i < array.Length; i++)
		{
			text += array[i].ToString(text2).PadLeft(2, '0');
		}
		return text;
	}
}
